package com.example.diandian.service.edu.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.diandian.common.base.result.R;
import com.example.diandian.service.edu.entity.Teacher;
import com.example.diandian.service.edu.entity.vo.TeacherQueryVo;
import com.example.diandian.service.edu.feign.OssFileService;
import com.example.diandian.service.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author JTJ
 * @since 2020-07-19
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
//@CrossOrigin
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("测试服务调用")
    @GetMapping("test")
    public R test(){
        ossFileService.test();
        return R.ok();
    }



    @ApiOperation("根据左关键字查询讲师列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(@ApiParam(value = "查询关键字", required = true)
                                     @PathVariable String key){
        List<Map<String, Object>> nameList = teacherService.selectNameListByKey(key);

        return R.ok().data("nameList", nameList);
    }

    @ApiOperation("根据id列表删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows(@ApiParam(value = "讲师id列表",required = true)
                        @RequestBody List<String> idList){
        boolean b = teacherService.removeByIds(idList);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("查询全部讲师")
    @GetMapping("list")
    public R listAll() {
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据ID删除讲师", notes = "根据ID删除讲师，逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id){

        //删除图片
        teacherService.removeAvatarById(id);
        //删除讲师
        boolean result = teacherService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("分页查询教师列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                      @ApiParam("查询对象") TeacherQueryVo teacherQueryVo) {
        Page<Teacher> page1 = new Page<Teacher>(page, limit);
        Page<Teacher> pages = teacherService.selectPage(page1, teacherQueryVo);
        List<Teacher> records = pages.getRecords();
        long total = pages.getTotal();
        return R.ok().data("rows", records).data("total", total);
    }

    @ApiOperation("保存讲师信息")
    @PostMapping("save")
    public R save(@ApiParam("讲师信息") @RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.ok().message("新增成功");
        } else {
            return R.error().message("新增失败");
        }
    }

    @ApiOperation("获取讲师")
    @GetMapping("get/{id}")
    public R get(@ApiParam("讲师id") @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return R.ok().data("item", teacher);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("修改讲师")
    @PutMapping("update")
    public R updateById(@ApiParam("讲师对象") @RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("数据不存在");
        }
    }
}

